/*
* Poppy Game Engine - https://pgetemplate.googlecode.com/svn/
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* Copyright (c) 2008 Poppy
*/

#pragma once

#ifdef WIN32
#include <d3d9.h>
#endif


#ifndef PSP_SCREEN_WIDTH
#define PSP_SCREEN_WIDTH	480
#endif

#ifndef PSP_SCREEN_HEIGHT
#define PSP_SCREEN_HEIGHT	272
#endif

#ifndef PSP_BUFFER_WIDTH
#define PSP_BUFFER_WIDTH	512
#endif

#ifndef PSP_PIXEL_SIZE
#define PSP_PIXEL_SIZE 4
#endif

#ifndef PSP_FRAME_SIZE
#define PSP_FRAME_SIZE 557056
#endif

#ifndef PIXEL_SIZE
#define PIXEL_SIZE 4
#endif

#ifndef M_PI
#define M_PI	3.14159265358979323846f
#define M_PI_2	1.57079632679489661923f
#define M_PI_4	0.785398163397448309616f
#define M_1_PI	0.318309886183790671538f
#define M_2_PI	0.636619772367581343076f
#endif

#define RAD2DEG(x)		(x*57.29577951f)
#define DEG2RAD(x)		(x*0.017453293f)

#ifdef WIN32

#ifndef WIN_WIDTH_OFFSET
#define WIN_WIDTH_OFFSET 8
#endif

#ifndef WIN_HEIGHT_OFFSET
#define WIN_HEIGHT_OFFSET 34
#endif

#endif

#ifdef WIN32
#define LPINDEX_BUFFER LPDIRECT3DINDEXBUFFER9
#define LPVERTEX_BUFFER LPDIRECT3DVERTEXBUFFER9
#define PGEPRIMITIVE_TYPE unsigned int

struct VERTEX2D
{
	float x, y, z, rhw;
	unsigned int color;
};

#define VERTEX_TYPE_2D_COLOR (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

#endif

#ifdef PSP
#define LPINDEX_BUFFER void*
#define LPVERTEX_BUFFER void*
#define PGEPRIMITIVE_TYPE unsigned int

struct VERTEX2D
{
	unsigned int color;
	unsigned short x,y,z;
	unsigned short pad;
};

#define VERTEX_TYPE_2D_COLOR (PGE_COLOR_8888|PGE_VERTEX_16BIT|PGE_TRANSFORM_2D)

#endif

#ifdef WIN32
#define PGE_ARGB(a,r,g,b)	(((a) << 24)|((r) << 16)|((g) << 8)|(b))
#define PGE_ABGR(a,b,g,r)	PGE_ARGB(a,r,g,b)
#endif
#ifdef PSP
#define PGE_ARGB(a,r,g,b)	PGE_ABGR(a,b,g,r)
#define PGE_ABGR(a,b,g,r)	(((a) << 24)|((b) << 16)|((g) << 8)|(r))
#endif
#define PGE_RGBA(r,g,b,a)	PGE_ARGB(a,r,g,b)
#define PGE_RGB(r,g,b)		PGE_ARGB(255,r,g,b)

//PSM
#ifdef WIN32
#define PGE_FORMAT int
#endif
#ifdef PSP
#define PGE_FORMAT int
#define PGE_PSM_4444   (2)
#define PGE_PSM_5551   (1)
#define PGE_PSM_5650   (0)
#define PGE_PSM_8888   (3)
#define PGE_PSM_DXT1   (8)
#define PGE_PSM_DXT3   (9)
#define PGE_PSM_DXT5   (10)
#define PGE_PSM_T16   (6)
#define PGE_PSM_T32   (7)
#define PGE_PSM_T4   (4)
#define PGE_PSM_T8   (5)
#endif

//Clear Area
#ifdef WIN32
#define PGE_CLEAR_COLOR D3DCLEAR_TARGET
#define PGE_CLEAR_DEPTH D3DCLEAR_ZBUFFER
#define PGE_CLEAR_STENCIL D3DCLEAR_STENCIL
#endif
#ifdef PSP
#define PGE_CLEAR_COLOR 1
#define PGE_CLEAR_DEPTH 2
#define PGE_CLEAR_STENCIL 4
#endif

//Front face
#ifdef PSP
#define PGE_CW (0)
#define PGE_CCW (1)
#endif

#ifdef PSP
#define PGE_SMOOTH (1)
#endif

//PresentationInerval
#ifdef WIN32
#define PRESENTATION_INTERVAL unsigned int
#endif
#ifdef PSP
#define PRESENTATION_INTERVAL unsigned int
#define IMMEDIATE 0
#define NEXTFRAME 1
#endif

//FVF
#ifdef WIN32
typedef enum
{
	PGE_POINTLIST = 1,
	PGE_LINELIST = 2,
	PGE_LINESTRIP = 3,
	PGE_TRIANGLELIST = 4,
	PGE_TRIANGLESTRIP = 5,
	PGE_TRIANGLEFAN = 6,
} PRIMITIVE;
#endif
#ifdef PSP

typedef enum
{
	PGE_POINTLIST = 0,
	PGE_LINELIST = 1,
	PGE_LINESTRIP = 2,
	PGE_TRIANGLELIST = 3,
	PGE_TRIANGLESTRIP = 4,
	PGE_TRIANGLEFAN = 5,
	PGE_SPRITES = 6
} PRIMITIVE;

typedef enum
{
	PGE_TRANSFORM_3D = 0x000000,
	PGE_TRANSFORM_2D = 0x800000,
	
	PGE_INDEX_16BIT = 0x1000,
	PGE_INDEX_8BIT = 0x800,
	
	PGE_WEIGHT_32BITF = 0x600,
	PGE_WEIGHT_16BIT = 0x400,
	PGE_WEIGHT_8BIT = 0x200,
	
	PGE_VERTEX_32BITF = 0x180,
	PGE_VERTEX_16BIT = 0x100,
	PGE_VERTEX_8BIT = 0x80,
	
	PGE_NORMAL_32BITF = 0x60,
	PGE_NORMAL_16BIT = 0x40,
	PGE_NORMAL_8BIT = 0x20,
	
	PGE_COLOR_8888 = 0x1C,
	PGE_COLOR_4444 = 0x18,
	PGE_COLOR_5551 = 0x14,
	PGE_COLOR_5650 = 0x10,
	
	PGE_TEXTURE_32BITF = 0x3,
	PGE_TEXTURE_16BIT = 0x2,
	PGE_TEXTURE_8BIT = 0x1
} VERTEX_TYPE;

#define PGE_WEIGHTS(n) ((((n)-1)&7)<<14)
#define PGE_VERTICES(n) ((((n)-1)&7)<<18)

#endif

//Color 
#define PGE_AMBIENT		0x1;
#define PGE_DIFFUSE		0x2;
#define PGE_SPECULAR	0x4;

//PSP GE Commands
#define NOP		0x00		//No Operation
#define VADDR	0x01		//Vertex List (BASE)
#define IADDR	0x02		//Index List (BASE)
#define PRIM	0x04		//Primitive Kick
#define BEZIER	0x05		//Bezier Patch Kick
#define SPLINE	0x06		//Spline Surface Kick
#define BBOX	0x07		//Bounding Box
#define JUMP	0x08		//Jump To New Address (BASE)
#define BJUMP	0x09		//Conditional Jump (BASE)
#define CALL	0x0A		//Call Address (BASE)
#define RET		0x0B		//Return From Call
#define END		0x0C		//Stop Execution
#define SIGNAL	0x0E		//Raise Signal Interrupt
#define FINISH	0x0F		//Complete Rendering
#define BASE	0x10		//Base Address Register
#define VTYPE	0x12		//Vertex Type
#define OFFA	0x13		//Offset Address (BASE)
#define ORIA	0x14		//Origin Address (BASE)
#define REGION1	0x15		//REGION1 Draw Region Start
#define REGION2	0x16		//REGION2 Draw Region End
#define LTE		0x17		//Lighting Enable
#define LTE0	0x18		//Light 0 Enable
#define LTE1	0x19		//Light 1 Enable
#define LTE2	0x1A		//Light 2 Enable
#define LTE3	0x1B		//Light 3 Enable
#define CPE		0x1C		//Clip Plane Enable
#define BCE		0x1D		//Backface Culling Enable
#define TME		0x1E		//Texture Mapping Enable
#define FGE		0x1F		//Fog Enable
#define DTE		0x20		//Dither Enable
#define ABE		0x21		//Alpha Blend Enable 
#define ATE		0x22		//Alpha Test Enable 
#define ZTE		0x23		//Depth Test Enable 
#define STE		0x24		//Stencil Test Enable 
#define AAE		0x25		//Anitaliasing Enable 
#define PCE		0x26		//Patch Cull Enable 
#define CTE		0x27		//Color Test Enable 
#define LOE		0x28		//Logical Operation Enable 
#define BOFS	0x2A		//Bone Matrix Offset 
#define BONE	0x2B		//Bone Matrix Upload 
#define MW0		0x2C		//Morph Weight 0 
#define MW1		0x2D		//Morph Weight 1 
#define MW2		0x2E		//Morph Weight 2 
#define MW3		0x2F		//Morph Weight 3 
#define MW4		0x30		//Morph Weight 4 
#define MW5		0x31		//Morph Weight 5 
#define MW6		0x32		//Morph Weight 6 
#define MW7		0x33		//Morph Weight 7 
#define PSUB	0x36		//Patch Subdivision 
#define PPRIM	0x37		//Patch Primitive 
#define PFACE	0x38		//Patch Front Face 
#define WMS		0x3A		//World Matrix Select 
#define WORLD	0x3B		//World Matrix Upload 
#define VMS		0x3C		//View Matrix Select 
#define VIEW	0x3D		//View Matrix upload 
#define PMS		0x3E		//Projection matrix Select 
#define PROJ	0x3F		//Projection Matrix upload 
#define TMS		0x40		//Texture Matrix Select 
#define TMATRIX	0x41		//Texture Matrix Upload 
#define XSCALE	0x42		//Viewport Width Scale 
#define YSCALE	0x43		//Viewport Height Scale 
#define ZSCALE	0x44		//Depth Scale 
#define XPOS	0x45		//Viewport X Position 
#define YPOS	0x46		//Viewport Y Position 
#define ZPOS	0x47		//Depth Position 
#define USCALE	0x48		//Texture Scale U 
#define VSCALE	0x49		//Texture Scale V 
#define UOFFSET	0x4A		//Texture Offset U 
#define VOFFSET	0x4B		//Texture Offset V 
#define OFFSETX	0x4C		//Viewport offset (X) 
#define OFFSETY	0x4D		//Viewport offset (Y) 
#define WMS		0x3A		//World Matrix Select 
#define WORLD	0x3B		//World Matrix Upload 
#define VMS		0x3C		//View Matrix Select 
#define VIEW	0x3D		//View Matrix upload 
#define PMS		0x3E		//Projection matrix Select 
#define PROJ	0x3F		//Projection Matrix upload 
#define TMS		0x40		//Texture Matrix Select 
#define TMATRIX	0x41		//Texture Matrix Upload 
#define XSCALE	0x42		//Viewport Width Scale 
#define YSCALE	0x43		//Viewport Height Scale 
#define ZSCALE	0x44		//Depth Scale 
#define XPOS	0x45		//Viewport X Position 
#define YPOS	0x46		//Viewport Y Position 
#define ZPOS	0x47		//Depth Position 
#define USCALE	0x48		//Texture Scale U 
#define VSCALE	0x49		//Texture Scale V 
#define UOFFSET	0x4A		//Texture Offset U 
#define VOFFSET	0x4B		//Texture Offset V 
#define OFFSETX	0x4C		//Viewport offset (X) 
#define OFFSETY	0x4D		//Viewport offset (Y) 
#define SHADE	0x50		//Shade Model 
#define RNORM	0x51		//Reverse Face Normals Enable 
#define CMAT	0x53		//Color Material 
#define EMC		0x54		//Emissive Model Color 
#define AMC		0x55		//Ambient Model Color 
#define DMC		0x56		//Diffuse Model Color 
#define SMC		0x57		//Specular Model Color 
#define AMA		0x58		//Ambient Model Alpha 
#define SPOW	0x5B		//Specular Power 
#define ALC		0x5C		//Ambient Light Color 
#define ALA		0x5D		//Ambient Light Alpha 
#define LMODE	0x5E		//Light Model 
#define LT0		0x5F		//Light Type 0 
#define LT1		0x60		//Light Type 1 
#define LT2		0x61		//Light Type 2 
#define LT3		0x62		//Light Type 3 
#define LXP0	0x63		//Light X Position 0 
#define LYP0	0x64		//Light Y Position 0 
#define LZP0	0x65		//Light Z Position 0 
#define LXP1	0x66		//Light X Position 1 
#define LYP1	0x67		//Light Y Position 1 
#define LZP1	0x68		//Light Z Position 1 
#define LXP2	0x69		//Light X Position 2 
#define LYP2	0x6A		//Light Y Position 2 
#define LZP2	0x6B		//Light Z Position 2 
#define LXP3	0x6C		//Light X Position 3 
#define LYP3	0x6D		//Light Y Position 3 
#define LZP3	0x6E		//Light Z Position 3 
#define LXD0	0x6F		//Light X Direction 0 
#define LYD0	0x70		//Light Y Direction 0 
#define LZD0	0x71		//Light Z Direction 0 
#define LXD1	0x72		//Light X Direction 1 
#define LYD1	0x73		//Light Y Direction 1 
#define LZD1	0x74		//Light Z Direction 1 
#define LXD2	0x75		//Light X Direction 2 
#define LYD2	0x76		//Light Y Direction 2 
#define LZD2	0x77		//Light Z Direction 2 
#define LXD3	0x78		//Light X Direction 3 
#define LYD3	0x79		//Light Y Direction 3 
#define LZD3	0x7A		//Light Z Direction 3 
#define LCA0	0x7B		//Light Constant Attenuation 0 
#define LLA0	0x7C		//Light Linear Attenuation 0 
#define LQA0	0x7D		//Light Quadratic Attenuation 0 
#define LCA1	0x7E		//Light Constant Attenuation 1 
#define LLA1	0x7F		//Light Linear Attenuation 1 
#define LQA1	0x80		//Light Quadratic Attenuation 1 
#define LCA2	0x81		//Light Constant Attenuation 2 
#define LLA2	0x82		//Light Linear Attenuation 2 
#define LQA2	0x83		//Light Quadratic Attenuation 2 
#define LCA3	0x84		//Light Constant Attenuation 3 
#define LLA3	0x85		//Light Linear Attenuation 3 
#define LQA3	0x86		//Light Quadratic Attenuation 3 
#define SLTE0	0x87		//Spot light 0 exponent 
#define SLTE1	0x88		//Spot light 1 exponent 
#define SLTE2	0x89		//Spot light 2 exponent 
#define SLTE3	0x8A		//Spot light 3 exponent 
#define SLTC0	0x8B		//Spot light 0 cutoff 
#define SLTC1	0x8C		//Spot light 1 cutoff 
#define SLTC2	0x8D		//Spot light 2 cutoff 
#define SLTC3	0x8E		//Spot light 3 cutoff 
#define ALC0	0x8F		//Ambient Light Color 0 
#define DLC0	0x90		//Diffuse Light Color 0 
#define SLC0	0x91		//Specular Light Color 0 
#define ALC1	0x92		//Ambient Light Color 1 
#define DLC1	0x93		//Diffuse Light Color 1 
#define SLC1	0x94		//Specular Light Color 1 
#define ALC2	0x95		//Ambient Light Color 2 
#define DLC2	0x96		//Diffuse Light Color 2 
#define SLC2	0x97		//Specular Light Color 2 
#define ALC3	0x98		//Ambient Light Color 3 
#define DLC3	0x99		//Diffuse Light Color 3 
#define SLC3	0x9A		//Specular Light Color 3 
#define FFACE	0x9B		//Front Face Culling Order 
#define FBP		0x9C		//Frame Buffer Pointer 
#define FBW		0x9D		//Frame Buffer Width 
#define ZBP		0x9E		//Depth Buffer Pointer 
#define ZBW		0x9F		//Depth Buffer Width 
#define TBP0	0xA0		//Texture Buffer Pointer 0 
#define TBP1	0xA1		//Texture Buffer Pointer 1 
#define TBP2	0xA2		//Texture Buffer Pointer 2 
#define TBP3	0xA3		//Texture Buffer Pointer 3 
#define TBP4	0xA4		//Texture Buffer Pointer 4 
#define TBP5	0xA5		//Texture Buffer Pointer 5 
#define TBP6	0xA6		//Texture Buffer Pointer 6 
#define TBP7	0xA7		//Texture Buffer Pointer 7 
#define TBW0	0xA8		//Texture Buffer Width 0 
#define TBW1	0xA9		//Texture Buffer Width 1 
#define TBW2	0xAA		//Texture Buffer Width 2 
#define TBW3	0xAB		//Texture Buffer Width 3 
#define TBW4	0xAC		//Texture Buffer Width 4 
#define TBW5	0xAD		//Texture Buffer Width 5 
#define TBW6	0xAE		//Texture Buffer Width 6 
#define TBW7	0xAF		//Texture Buffer Width 7 
#define CBP		0xB0		//CLUT Buffer Pointer 
#define CBPH	0xB1		//CLUT Buffer Pointer H 
#define TRXSBP	0xB2		//Transmission Source Buffer Pointer 
#define TRXSBW	0xB3		//Transmission Source Buffer Width 
#define TRXDBP	0xB4		//Transmission Destination Buffer Pointer 
#define TRXDBW	0xB5		//Transmission Destination Buffer Width  
#define TSIZE0	0xB8		//Texture Size Level 0 
#define TSIZE1	0xB9		//Texture Size Level 1 
#define TSIZE2	0xBA		//Texture Size Level 2 
#define TSIZE3	0xBB		//Texture Size Level 3 
#define TSIZE4	0xBC		//Texture Size Level 4 
#define TSIZE5	0xBD		//Texture Size Level 5 
#define TSIZE6	0xBE		//Texture Size Level 6 
#define TSIZE7	0xBF		//Texture Size Level 7 
#define TMAP	0xC0		//Texture Projection Map Mode + Texture Map Mode 
#define TEMM	0xC1		//Texture Environment Map Matrix 
#define TMODE	0xC2		//Texture Mode 
#define TPSM	0xC3		//Texture Pixel Storage Mode 
#define CLOAD	0xC4		//CLUT Load 
#define CMODE	0xC5		//CLUT Mode 
#define TFLT	0xC6		//Texture Filter 
#define TWRAP	0xC7		//Texture Wrapping 
#define TBIAS	0xC8		//Texture Level Bias (???) 
#define TFUNC	0xC9		//Texture Function 
#define TEC		0xCA		//Texture Environment Color 
#define TFLUSH	0xCB		//Texture Flush 
#define TSYNC	0xCC		//Texture Sync 
#define FFAR	0xCD		//Fog Far (???) 
#define FDIST	0xCE		//Fog Range 
#define FCOL	0xCF		//Fog Color 
#define TSLOPE	0xD0		//Texture Slope 
#define PSM		0xD2		//Frame Buffer Pixel Storage Mode 
#define CLEAR	0xD3		//Clear Flags 
#define SCISOR1	0xD4		//Scissor Region Start 
#define SCISOR2	0xD5		//SCISSOR2 Scissor Region End 
#define NEARZ	0xD6		//Near Depth Range 
#define FARZ	0xD7		//Far Depth Range 
#define CTST	0xD8		//Color Test Function 
#define CREF	0xD9		//Color Reference 
#define CMSK	0xDA		//Color Mask 
#define ATST	0xDB		//Alpha Test 
#define STST	0xDC		//Stencil Test 
#define SOP		0xDD		//Stencil Operations 
#define ZTST	0xDE		//Depth Test Function 
#define ALPHA	0xDF		//Alpha Blend 
#define SFIX	0xE0		//Source Fix Color 
#define DFIX	0xE1		//Destination Fix Color 
#define DTH0	0xE2		//Dither Matrix Row 0 
#define DTH1	0xE3		//Dither Matrix Row 1 
#define DTH2	0xE4		//Dither Matrix Row 2 
#define DTH3	0xE5		//Dither Matrix Row 3 
#define LOP		0xE6		//Logical Operation 
#define ZMSK	0xE7		//Depth Mask 
#define PMSKC	0xE8		//Pixel Mask Color 
#define PMSKA	0xE9		//Pixel Mask Alpha 
#define TRXKICK	0xEA		//Transmission Kick 
#define TRXSPOS	0xEB		//Transfer Source Position 
#define TRXDPOS	0xEC		//Transfer Destination Position      
#define TRXSIZE	0xEE		//Transfer Size 
